﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class DivisionsDataAccess: DataAccessBase
    {
        public DivisionsDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Division> SelectDivisionsList()
        {
            //Select Tobject List
            List<TObject> tobjList = SelectTObjectsList();

            //Select Divisions List
            List<DIVISION> divList =  dataContext.DIVISION.ToList();

            //Merge result
            var res = (from tobj in tobjList
                       join div in divList on tobj.ID equals div.ID
                      select new Division
                      {
                          DataBaseName = dataBaseName,
                          CLASS_ID = tobj.CLASS_ID,
                          EXT_ID = tobj.EXT_ID,
                          ID = tobj.ID,
                          NAME = tobj.NAME,
                          PARENT_ID = tobj.PARENT_ID,
                          BOSS_ID = div.BOSS_ID,
                          DATE_CREATED = div.DATE_CREATED ?? DateTime.Now,
                          DESCRIPTION = div.DESCRIPTION
                      }).ToList();           

            return res;
        }

        public Division SelectDivisionBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                TObject basePart = SelectTObject_By_Id(Id);

                //Select Division part
                List<DIVISION> divisionPart = dataContext.DIVISION.ToList();

                //Merge result
                var res = (from div in divisionPart
                           where div.ID == basePart.ID
                           select new Division
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = div.BOSS_ID,
                               DATE_CREATED = div.DATE_CREATED ?? DateTime.Now,
                               DESCRIPTION = div.DESCRIPTION
                           }).FirstOrDefault();

                var boss = dataContext.TOBJECT.Where(t => t.EXT_ID == res.BOSS_ID).FirstOrDefault();
                res.BOSS_NAME = boss !=null ? boss.NAME : String.Empty; 

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectDivisionById");
            }
        }

        public void InsertDivision(Division division,ref int generated_Id)
        {
            try
            {   
                //Insert the TObject part
                InsertTObject( division, ref generated_Id );

                //Insert the Division part
                int new_Id = generated_Id;
                DIVISION divisionPart = new DIVISION();
                divisionPart.TOBJECT = dataContext.TOBJECT.Where(t => t.ID == new_Id).FirstOrDefault();
                divisionPart.ID = new_Id;
                divisionPart.BOSS_ID = division.BOSS_ID;
                divisionPart.DATE_CREATED = division.DATE_CREATED;
                divisionPart.DESCRIPTION = division.DESCRIPTION;

                //Submit changes
                dataContext.AddToDIVISION(divisionPart);
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while InsertDivision");
            }
        }

        public void UpdateDivision(Division division)
        {
            try
            {
                //Update the TObject part
                UpdateTObject(division);

                //Update the Division part
                DIVISION divisionPart = dataContext.DIVISION.Where(t => t.ID == division.ID).FirstOrDefault();

                divisionPart.BOSS_ID = division.BOSS_ID;
                divisionPart.DATE_CREATED = division.DATE_CREATED;
                divisionPart.DESCRIPTION = division.DESCRIPTION;
                
                //Submit changes
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while UpdateDivision");
            }
        }

        public void DeleteDivision(Division division)
        {
            try
            {
                //Delete the Division part
                DIVISION divPart = dataContext.DIVISION.Where(t => t.ID == division.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();


                //Delete the TObject part
                DeleteTObject(division);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteDivision");
            }
        }
    }
}
